package resp

import "github.com/gin-gonic/gin"

type RESTError interface {
	GetStatus() int
	GetCode() int
	GetMessage() string
}

type DatabaseError interface {
	GetCode() int
	GetMessage() string
}

func ErrorHandler(c *gin.Context, err error) {
	switch err := err.(type) {
	case RESTError:
		c.AbortWithStatusJSON(err.GetStatus(), NewFailedRespBody(err.GetCode(), err.GetMessage()))
	case DatabaseError:
		InternalServerError(c, err.GetCode(), err.GetMessage())
	default:
		InternalServerError(c, CodeServerError, "Unknown Internal Server Error")
	}
}

func ParseRESTError(err error) RESTError {
	return err.(RESTError)
}

func ParseDatabaseError(err error) DatabaseError {
	return err.(DatabaseError)
}
